﻿using System;
using System.Windows.Forms;
using Plugins.Controls.Interfaces;

namespace Plugins.Controls
{
    [Serializable]
    public class PluginListViewItemComparer : IPluginListViewItemComparer
    {
        private int _columnIndex;
        private SortOrder _sortOrder;
        public PluginListViewItemComparer(int column, SortOrder order)
        {
            _columnIndex = column;
            _sortOrder = order;
        }
        public int ColumnIndex { get { return _columnIndex; } set { _columnIndex = value; } }
        public SortOrder SortOrder { get { return _sortOrder; } set { _sortOrder = value; } }
        public int Compare(object x, object y)
        {
            switch (_sortOrder)
            {
                case SortOrder.Ascending:
                    return string.Compare(((ListViewItem)x).SubItems[_columnIndex].Text, ((ListViewItem)y).SubItems[_columnIndex].Text);
                case SortOrder.Descending:
                    return string.Compare(((ListViewItem)y).SubItems[_columnIndex].Text, ((ListViewItem)x).SubItems[_columnIndex].Text);
                default:
                    return 0;
            }
        }
    }
}
